﻿using DevComponents.DotNetBar;
using Elike.Toolkit.ControlsErgodic.Windows;
using shopsoft.main.AppCommon;
using shopsoft.main.MemberManage;
using shopsoft.models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace shopsoft.main.CustomSearch
{
    public partial class MemberSearch : Balloon
    {
        #region Public Attribute

        public SearchWhere where;

        #endregion Public Attribute

        /// <summary>
        /// 构造函数
        /// </summary>
        public MemberSearch()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MemberSearch_Load(object sender, EventArgs e)
        {
            //
            BindMemberGrade();
        }

        /// <summary>
        /// 窗口停用时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MemberSearch_Deactivate(object sender, EventArgs e)
        {
            //
            Hide();
        }

        #region Method

        /// <summary>
        /// 绑定会员等级
        /// </summary>
        private void BindMemberGrade()
        {
            var memberGrades = new List<MemberGrade>();
            memberGrades.AddRange(ParameterConfig.GetMemberGrades());
            memberGrades[0].Name = "全部";
            ComboBoxErgodic.DataBind(cmbMemberGrade, memberGrades, "ID", "Name");
        }

        #endregion

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            //
            if (Owner != null)
            {
                //if条件需具体问题具体分析
                where = new SearchWhere();
                if (cmbMemberGrade.SelectedItem != null
                    || txtKey.Text.Trim() != string.Empty || dateStart.Value != ClientParameterSettings.InvalidDateTime ||
                    dateEnd.Value != ClientParameterSettings.InvalidDateTime)
                {
                    //关键字
                    where.KeyWords = new[]
                    {
                        txtKey.Text.Trim()
                    };
                    //日期
                    DateTime?[] dateTimes = new DateTime?[2];
                    if (dateStart.Value != ClientParameterSettings.InvalidDateTime)
                        dateTimes[0] = dateStart.Value;
                    if (dateEnd.Value != ClientParameterSettings.InvalidDateTime)
                        dateTimes[1] = dateEnd.Value;
                    where.DateTimeIntervals = new List<DateTime?[]>()
                    {
                        dateTimes
                    };

                    if (cmbMemberGrade.SelectedValue != null)
                    {
                        where.MemberGradeID = cmbMemberGrade.SelectedValue as int?;
                    }
                }
                else
                {
                    where = null;
                }

                //删除状态
                if (rdoNoDelete.Checked)
                {
                    where.isDeleted = false;
                }
                else
                {
                    where.isDeleted = true;
                }

                ((MemberInfo)Owner).Search();
            }
        }
    }
}
